package io.gitee.chearnee.fw.openapi.exception;

import io.gitee.chearnee.fw.openapi.OpenApiAutoConfiguration;
import io.gitee.chearnee.fw.openapi.result.OpenApiResult;
import io.gitee.chearnee.fw.web.annotation.EnableResponseAdvice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 异常增强处理
 *
 * @author Smile
 * @date 2018-12-28
 */
@ResponseBody
@ControllerAdvice
@EnableResponseAdvice(enable = false)
@Slf4j
public class OpenApiExceptionHandler {

  @ExceptionHandler(OpenApiException.class)
  public OpenApiResult openApiException(OpenApiException e) {
    log.warn("OpenApiException", e);
    return OpenApiResult.fail(e);
  }

  @ExceptionHandler(RuntimeException.class)
  @ConditionalOnClass(OpenApiAutoConfiguration.class)
  public OpenApiResult commonException(RuntimeException e) {
    log.warn("RuntimeException", e);
    return OpenApiResult.fail(e.getMessage());
  }
}
